package com.central.operate.jobs;

import com.central.operate.dao.AccountDao;
import com.central.operate.model.OperateRecord;
import com.central.operate.service.LoginCountService;
import com.central.operate.service.OperateRecordService;
import com.central.operate.service.RegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**定时任务*/
@Slf4j
@Component
public class Jobs {

    /**一分钟*/
    public final static long ONE_MINUTE =  60 * 1000;
    /**一小时*/
    public final static long ONE_HOUR =  60 * ONE_MINUTE;
    /**一天*/
    public final static long ONE_DAY =  24 * ONE_HOUR;
    /**4*/
    public final static long FOUR =  4;
    /**4分钟容错*/
    public final static long FOUR_MINUTE =  FOUR * ONE_MINUTE;

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private OperateRecordService operateRecordService;

    @Autowired
    private LoginCountService loginCountService;

    @Autowired
    private RegisterService registerService;

    /**
     * 按小时统计
     * 在下一个时间点统计前一个小时的数据
     * 例如:在2:10运行,统计1:00~2:00的数据
     */
    @Scheduled(fixedDelay=ONE_HOUR)
    public void cronJob(){
        log.info("执行定时任务------");
        Date date = new Date();
        Map<String, Object> params = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String endTime = sdf.format((date.getTime()/ONE_HOUR)*ONE_HOUR);
        String startTime = sdf.format((date.getTime()/ONE_HOUR)*ONE_HOUR - ONE_HOUR);
        log.info("起始时间:" + startTime);
        log.info("终止时间:" + endTime);
        params.put("endTime",endTime);
        params.put("startTime",startTime);
        /*
        在线人数
         */
        Integer onlineHistory = loginCountService.countOnlineHistory(params);
        log.info("在线人数:" + onlineHistory);
        /*
        注册玩家
         */
        Integer registerCount = registerService.findRegisterOneHour(params);
        log.info("注册人数:" + registerCount);
        /*
        登录量
         */
        Integer loginCount = loginCountService.countLoginCountHistory(params);
        log.info("登录量:" + loginCount);
        /*
        保存数据
         */
        OperateRecord operateRecord = new OperateRecord(startTime,endTime,onlineHistory,registerCount,loginCount);
        try{
            operateRecordService.saveOneHourRecord(operateRecord);
        }catch (Exception e){
            log.info("定时任务数据插入出错");
        }
        log.info("定时任务完毕------");
    }

    private static boolean isThisTime(long time,String pattern) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String param = sdf.format(date);//参数时间
        String now = sdf.format(new Date());//当前时间
        System.out.println(param);
        System.out.println(now);
        if(param.equals(now)){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
//        System.out.println(isThisTime(new Date().getTime() - ONE_HOUR,"yyyy-MM-dd HH"));
        /*Date date = new Date();
        System.out.println(date.getTime());
        System.out.println((date.getTime()/ONE_HOUR)*ONE_HOUR);
        System.out.println("------------");
        System.out.println(date);
        System.out.println(new Date((date.getTime()/ONE_HOUR)*ONE_HOUR - ONE_HOUR));*/

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String param = sdf.format(date);
        System.out.println(param);
    }
}
